//
//  PKPlayListTableViewController.m
//  PianKe
//
//  Created by ybq on 15/12/25.
//  Copyright © 2015年 B7_Young. All rights reserved.
//

#import "PKPlayListTableViewController.h"
#import "PKPlayListTableViewCell.h"

@interface PKPlayListTableViewController ()

@property (nonatomic, strong) PKMusicPlayer *player;

@end

@implementation PKPlayListTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self.tableView registerClass:[PKPlayListTableViewCell class] forCellReuseIdentifier:@"PKPlayListTableViewCell"];
    self.tableView.rowHeight = 55;
    _player = [PKMusicPlayer sharePlayer];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewDidLayoutSubviews {
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }  
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _listArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PKPlayListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PKPlayListTableViewCell" forIndexPath:indexPath];
    cell.model = _listArray[indexPath.row];
    // 点击cell不再有阴影
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.downLoadButton.tag = indexPath.row + 100;
    [cell.downLoadButton addTarget:self action:@selector(downLoad:) forControlEvents:(UIControlEventTouchUpInside)];
    PKDownLoad *load = [[PKDownLoadManager sharedManager] downLoadFrom:cell.model];
    if (load == nil) {
        // 显示按钮 // 隐藏label
    } else {
        // 显示label  // 隐藏按钮
//        load就是这个单元格对应的下载任务
        if (load.isDownLoading) {
            // label显示百分比
            // 使用KVO的方式监听load的totalBytesWritten属性
            
        } else {
            // label显示暂停
            
        }
    }
// 点击cell显示状态
    if (indexPath.row == _player.index) {
        [cell.stateLabel setBackgroundColor:[UIColor colorWithRed:137.0 / 255 green:191.0 / 255 blue:93.0 / 255 alpha:1]];
    } else {
        [cell.stateLabel setBackgroundColor:[UIColor whiteColor]];
    }
    
    return cell;
}
// 点击下载按钮后执行的方法
- (void)downLoad:(UIButton *)button {
    // 根据被点击的按钮的tag算出这个按钮在第几个cell上面.取到cell对应的model,然后去创建一个下载任务
    PKDownLoad *downLoad = [[PKDownLoad alloc] initWithModel:_listArray[button.tag - 100]];
    [[PKDownLoadManager sharedManager] addDownLoadToManager:downLoad];
    // 刷新某几个单元格
    [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:button.tag - 100 inSection:0]] withRowAnimation:(UITableViewRowAnimationTop)];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger index = indexPath.row;
    if (indexPath.row != _player.index) {
        NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:_player.index inSection:0];
        PKPlayListTableViewCell *oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];
        [oldCell.stateLabel setBackgroundColor:[UIColor whiteColor]];
        
        PKPlayListTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        [cell.stateLabel setBackgroundColor:[UIColor colorWithRed:137.0 / 255 green:191.0 / 255 blue:93.0 / 255 alpha:1]];
        
        if (_delegate && [_delegate respondsToSelector:@selector(changeWithIndex:)]) {
            [_delegate changeWithIndex:index];
        }

        
    }
    
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
